package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ProductType;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.mapper.ProductTypeMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IProductService;
import cn.wolfcode.trip.base.service.IProductTypeService;
import cn.wolfcode.trip.base.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageInfo<Product> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Product> list = productMapper.selectForList(qo);
        return new PageInfo<>(list);
    }
    @Override
    public void saveOrUpdate(Product entity) {
        if(entity.getId()!=null){
            productMapper.updateByPrimaryKey(entity);
        }else {
            //添加时间
            Date date = new Date();
            entity.setAddTime(date);
            productMapper.insert(entity);
        }
    }

    @Override
    public PageInfo<Product> selectProductById(Long id,QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Product> list = productMapper.selectProductById(id,qo);
        return new PageInfo<>(list);
    }

    @Override
    public Product selectById(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Product> selectByDate(QueryObject qo) {
        Date beginDate = null;
        Date endDate = new Date();
        beginDate = DateUtil.getSevenDaysAgo(endDate);
        List<Product> list = productMapper.selectCommend(beginDate, endDate,qo);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Product> bathchGet(Long[] ids,QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Product> list = new ArrayList<>();
        for (Long id : ids) {
            Product product = productMapper.selectByPrimaryKey(id);
            list.add(product);
        }
        return new PageInfo<>(list);
    }

    @Override
    public List<Product> bathchGetList(Long[] ids) {
        List<Product> list = new ArrayList<>();
        for (Long id : ids) {
            Product product = productMapper.selectByPrimaryKey(id);
            list.add(product);
        }
        return list;
    }

    @Override
    public List<Product> selectByMoney() {
        return productMapper.selectByMoney();
    }

}
